package com.qch.demo_command;

/**
 * @Author qch
 * @Date 2021/8/12
 * 死锁
 */
public class Demo8jconsole03 {
    /*** 线程死锁等待演示 */
    static class SynAddRunalbe implements Runnable {
        int a, b;

        public SynAddRunalbe(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public void run() {
            synchronized (Integer.valueOf(a)) { //-127~128是从对应缓存(常量池)里去拿
                synchronized (Integer.valueOf(b)) {
                    System.out.println(a + b);
                }
            }
        }
    }

    public static void main(String[] args) {
        //a和b一样时就会发生死锁
        for (int i = 0; i < 100; i++) {
            new Thread(new SynAddRunalbe(1, 2)).start();
            new Thread(new SynAddRunalbe(2, 1)).start();
        }
    }
}
